/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import jobportal.EmailSender;

/**
 *
 * @author Ravindra
 */
public class ForgotPassword extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("student_forgotpassword.jsp");
        //String to = "", subject = "RecruitmentPortal Password", msg = "Your Login Password for RecruitmentPortal is :: ", password = "";
        String password = "";
        try {
            Connection con = Dbconn.getConn();
            Statement st = con.createStatement();
            String sql;
            //email=request.getParameter("email");
            // out.println(email);

            String email = "", subject = "", content = "", name = "", username = "";

            // to = request.getParameter("email");
            sql = "select email,password,name,username from student where username='" + request.getParameter("username") + "'";
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                email = rs.getString("email");
                password = rs.getString("password");
                name = rs.getString("name");
                username = rs.getString("username");
            } else {
                request.setAttribute("status", "<font color='red'>Please Enter correct user id </font>" + request.getParameter("username"));
                rd.forward(request, response);
            }
            rs = st.executeQuery("select * from emailmessage where id=17");
            if (rs.next()) {
                subject = rs.getString("subject");
                content = rs.getString("content");
                content = content.replace("%uname%", name);
                content = content.replace("%username%", username);
                content = content.replace("%password%", password);
                EmailSender.sendMail(email, subject, content);
                request.setAttribute("status", "<font color='green' >Email successfully sent to your email </font>");
                rd.forward(request, response);
            } else {
                request.setAttribute("status", "<font color='red'>Email can not be sent! </font>");
                rd.forward(request, response);
            }





        } catch (Exception e) {
            out.println(e);
//ss            rd.forward(request, response);
        } finally {
            out.close();

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
